﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using Common.Util;
using GxTsSharp.Models;
using log4net;

namespace StockIndex.service
{
    //涨速计算
    public class ZhangSuService
    {
        public static readonly int OneMinute = 1; //1分钟
        public static readonly int FiveMinute = 5; //分钟
        public static readonly int MinuteTicksCount = 20;

        private static readonly ILog Log = LogManager.GetLogger(typeof(ZhangSuService));

        /// <summary>
        /// 计算涨速,定时任务调用，每3秒一次
        /// </summary>
           
        public static void CalculateTopicZhangSu()
        {
            int count = MemoryDataDB.DicTopicZdf.Select(s => s.Value != 0).Count();
            if (count == 0)
            {
                Log.Info("主题涨跌幅数据还没初始化完成");
                return;
            }
            foreach (KeyValuePair<string, decimal> item in MemoryDataDB.DicTopicZdf)
            {
                string topicName = item.Key;
                decimal zdf = item.Value;
                LimitQueue<decimal> limitQueue = MemoryDataDB.DicFiveMinuteQueue.GetOrAdd(topicName,
                    s => new LimitQueue<decimal>(FiveMinute * MinuteTicksCount));
                if (limitQueue.Count() == 0 && zdf.Equals(0))
                {
                    continue;
                }
                limitQueue.Add(zdf);
                decimal zs = 0;
                decimal first = limitQueue.GetFirstData();
                decimal last = limitQueue.GetLastData();
                if (first != 0)
                {
                    zs = (last - first) / first;
                }

                MemoryDataDB.DicTopicZhangSu.AddOrUpdate(topicName, zs, (k, v) => zs);
            }
        }
    }
}